//
//  SellOrderDetailsViewController.m
//  HuiMWorld
//
//  Created by chirui on 2018/1/26.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "SellOrderDetailsViewController.h"
#import "OrderTableViewCell.h"

@interface SellOrderDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
}

@end

@implementation SellOrderDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"订单详情")
    
    NSLog(@"dataDic==%@",self.dataDic);
    
    [self creatTableView];
}


-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[OrderTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[OrderTableViewCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray * array = self.dataDic[@"products"];
    
    return array.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 210;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 450;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = self.dataDic[@"products"][indexPath.row];
    OrderTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[OrderTableViewCell reuseIdentifier] forIndexPath:indexPath];
    
    [cell.goodsImage sd_setImageWithURL:ImageUrl(dic[@"cover_photo"]) placeholderImage:ImageName(@"")];
    cell.goodsName.text = dic[@"product_name"];
    cell.goodsName.frame = CGRectMake(98, 10, WIDTH -110, 20);
    cell.goodsName.numberOfLines = 2;
    [cell.goodsName sizeToFit];
    cell.priceLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"price"]];
    cell.numberLabel.text = [NSString stringWithFormat:@"X%@",dic[@"number"]];
    
    return cell;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 210)];
    heardView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    
    UIView * statisView =[[UIView alloc]initWithFrame:CGRectMake((WIDTH-100)/2, 30, 100, 40)];
    statisView.backgroundColor = [UIColor clearColor];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 30, 30)];
    
    [statisView addSubview:imageView];
    
    
    if ([self.dataDic[@"status"] isEqualToString:@"wait"]) {
        imageView.image = ImageName(@"icon_order_Pack");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor blackColor] andText:@"待配送" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    if ([self.dataDic[@"status"] isEqualToString:@"deliver"]) {
        imageView.image = ImageName(@"icon_order_distribution");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor blackColor] andText:@"配送中" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    if ([self.dataDic[@"status"] isEqualToString:@"done"]) {
        imageView.image = ImageName(@"icon_lab_ok");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor blackColor] andText:@"已完成" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    UIView * addressView =[[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH, 100)];
    addressView.backgroundColor =[UIColor whiteColor];
    
    
    UIImageView * addressImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 75/2, 25, 25)];
    addressImageView.image = ImageName(@"icon_order_add");
    [addressView addSubview:addressImageView];
    
    [self creatLabelFrame:CGRectMake(35, 20, 120, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"address"][@"name"] andFont:15 andAliment:NSTextAlignmentLeft onView:addressView];
    
    [self creatLabelFrame:CGRectMake(160, 20, WIDTH - 170, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"address"][@"phone"] andFont:15 andAliment:NSTextAlignmentRight onView:addressView];
    
    [self creatLabelFrame:CGRectMake(35, 60, WIDTH - 40, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"address"][@"full_addr"] andFont:15 andAliment:NSTextAlignmentLeft onView:addressView];
    
    [heardView addSubview:addressView];
    
    [heardView addSubview:statisView];
    
    
    return heardView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView * footerView = [[UIView alloc]initWithFrame:CGRectMake(0,0,WIDTH, 450)];
    footerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView * priceView =[[UIView alloc]initWithFrame:CGRectMake(0, 10, WIDTH, 100)];
    priceView.backgroundColor =[UIColor whiteColor];
    
     [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"外卖金额" andFont:17 andAliment:NSTextAlignmentLeft onView:priceView];
    
       [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"￥%@",self.dataDic[@"order_price"]] andFont:16 andAliment:NSTextAlignmentRight onView:priceView];
    
    [self creatLabelFrame:CGRectMake(10, 65, 120, 20) andTextColor:[UIColor blackColor] andText:@"支付方式" andFont:16 andAliment:NSTextAlignmentLeft onView:priceView];
    
    [self creatLabelFrame:CGRectMake(130, 65, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"pay_type_string"]] andFont:16 andAliment:NSTextAlignmentRight onView:priceView];
    
    [footerView addSubview:priceView];
    
    UIView * orderView =[[UIView alloc]initWithFrame:CGRectMake(0, 120, WIDTH, 100)];
    orderView.backgroundColor =[UIColor whiteColor];
    
    [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"外卖编号" andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
    
    [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"order_sn"]] andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
    
    [self creatLabelFrame:CGRectMake(10, 65, 120, 20) andTextColor:[UIColor blackColor] andText:@"外卖变更时间" andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
    
    [self creatLabelFrame:CGRectMake(130, 65, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@",self.dataDic[@"update_time"]] andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
    
//    if ([self.dataDic[@"status"] isEqualToString:@"deliver"]||[self.dataDic[@"status"] isEqualToString:@"got"]) {
//        
//        UIView * emsView =[[UIView alloc]initWithFrame:CGRectMake(0, 230, WIDTH, 50)];
//        emsView.backgroundColor =[UIColor whiteColor];
//        
//        [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:[UIColor blackColor] andText:@"骑手" andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
//        
//        [self creatLabelFrame:CGRectMake(130, 15, WIDTH - 140, 20) andTextColor:[UIColor blackColor] andText:[NSString stringWithFormat:@"%@   %@",self.dataDic[@"deliver"][@"user_name"],self.dataDic[@"deliver"][@"phone"]] andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
//    
//        [footerView addSubview:emsView];
//    }
    
    [footerView addSubview:orderView];
    return footerView;
  
}


-(void)creatLabelFrame:(CGRect)rec andTextColor:(UIColor*)col andText:(NSString *)text andFont:(CGFloat)flo andAliment:(NSTextAlignment)aliment onView:(UIView *)view {
    
    
    UILabel * statisLabel =[[UILabel alloc]initWithFrame:rec];
    statisLabel.font = [UIFont systemFontOfSize:flo];
    statisLabel.textColor = col;
    statisLabel.textAlignment = aliment;
    statisLabel.text = text;
    [view addSubview:statisLabel];
    
    
}

@end
